<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class BuyController extends Controller
{
   public function buy() {
        //$car = new Car();
        
        $uri = $_SERVER['REQUEST_URI'];

        $arr = explode('/',$uri);
        unset($arr[0]);
        unset($arr[1]);


        $con = [];
        $con[] = '1';

        //品牌
        if(in_array('b0',$arr)) {
            unset($con['p_brand']);
        }
        if(in_array('b1',$arr)) {
            $con['p_brand'] = 'p_brand =1';
        }
        if(in_array('b2',$arr)) {
            $con['p_brand'] = 'p_brand =2';
        }
        if(in_array('b3',$arr)) {
            $con['p_brand'] = 'p_brand =3';
        }
        if(in_array('b4',$arr)) {
            $con['p_brand'] = 'p_brand =4';
        }
        if(in_array('b5',$arr)) {
            $con['p_brand'] = 'p_brand =5';
        }
        if(in_array('b6',$arr)) {
            $con['p_brand'] = 'p_brand =6';
        }
        if(in_array('b7',$arr)) {
            $con['p_brand'] = 'p_brand =7';
        }
        if(in_array('b8',$arr)) {
            $con['p_brand'] = 'p_brand =8';
        }
        if(in_array('b9',$arr)) {
            $con['p_brand'] = 'p_brand =9';
        }
        if(in_array('b10',$arr)) {
            $con['p_brand'] = 'p_brand =10';
        }
        if(in_array('b11',$arr)) {
            $con['p_brand'] = 'p_brand =11';
        }
        if(in_array('b12',$arr)) {
            $con['p_brand'] = 'p_brand =12';
        }
        if(in_array('b13',$arr)) {
            $con['p_brand'] = 'p_brand =13';
        }
        if(in_array('b14',$arr)) {
            $con['p_brand'] = 'p_brand =14';
        }
        if(in_array('b15',$arr)) {
            $con['p_brand'] = 'p_brand =15';
        }
        if(in_array('b16',$arr)) {
            $con['p_brand'] = 'p_brand =16';
        }
        if(in_array('b17',$arr)) {
            $con['p_brand'] = 'p_brand =17';
        }
        if(in_array('b18',$arr)) {
            $con['p_brand'] = 'p_brand =18';
        }

        //价格
        if(in_array('p0',$arr)) {
            unset($con['p_price']);
        }
        if(in_array('p1',$arr)) {
            $con['p_price'] = 'p_price <=5';
        }
        if(in_array('p2',$arr)) {
            $con['p_price'] = 'p_price >5 and p_price <=10';
        }
        if(in_array('p3',$arr)) {
            $con['p_price'] = 'p_price >10 and p_price <=15';
        }
        if(in_array('p4',$arr)) {
            $con['p_price'] = 'p_price >15 and p_price <=20';
        }
        if(in_array('p5',$arr)) {
            $con['p_price'] = 'p_price >20 and p_price <=30';
        }
        if(in_array('p6',$arr)) {
            $con['p_price'] = 'p_price >30 and p_price <=40';
        }
        if(in_array('p7',$arr)) {
            $con['p_price'] = 'p_price >40 and p_price <=60';
        }
        if(in_array('p8',$arr)) {
            $con['p_price'] = 'p_price >=60';
        }

        //车龄
        if(in_array('y0',$arr)) {
            unset($con['p_year']);
        }
        if(in_array('y1',$arr)) {
            $con['p_year'] = 'p_year < 2011';
        }
        if(in_array('y2',$arr)) {
            $con['p_year'] = 'p_year = 2011';
        }
        if(in_array('y3',$arr)) {
            $con['p_year'] = 'p_year = 2013';
        }
        if(in_array('y4',$arr)) {
            $con['p_year'] = 'p_year = 2016';
        }

        //设置默认值
        if(!isset($con['p_brand'])) {
            $arr[] = 'b0';
        }
        if(!isset($con['p_price'])) {
            $arr[] = 'p0';
        }
        if(!isset($con['p_year'])) {
            $arr[] = 'y0';
        }
        $where = implode(' and ',$con);
        
        //$carslist = $car::where($con)->get();
        $carslist = DB::select("select * from cars where $where");
        return view('Home.buy-index',['carslist'=>$carslist,'uri'=>$uri,'arr'=>$arr]);
    }
}
